<?php /* 	These code was generated using phpCIGen v 0.1.a (21/04/2009)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com, 
    #songbee	mukhlisona@gmail.com
	#CV. Trust Solution, jl. Saronojiwo 19 Surabaya, http://www.ts.co.id
	
	+ Module  		: users Model
	+ Description	: For record model process back-end
	+ Filename 		: c_users.php
 	+ Author  		: zainal, mukhlison
 	+ Created on 16/Jul/2009 15:35:27
	
*/

class M_tbl_s_users extends CI_Model{
		
		//constructor
		function M_tbl_s_users() {
			parent::__construct();
		}
		
		//function for get list record
		function users_list($filter,$start,$end){
			$query = "SELECT tbl_s_users.*, group_name, branch_name
						FROM tbl_s_users
						LEFT JOIN m_branch ON tbl_s_users.user_cabang = branch_id 
						LEFT JOIN tbl_s_usergroups ON user_groups = group_id
						WHERE user_groups=group_id 
						";
			
			// For simple search
			if ($filter<>""){
				$query .=preg_match("/\bWHERE\b/i",$query)? " AND ":" WHERE ";
				$query .= " (user_name LIKE '%".addslashes($filter)."%' )";
			}
			
			$result = $this->db->query($query);
			$nbrows = $result->num_rows();
			$limit = $query." LIMIT ".$start.",".$end;		
			$result = $this->db->query($limit);  
			
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}
		
		//function for update record
		function users_update($user_id ,$user_name ,$user_passwd ,$user_karyawan,$user_groups, 
							  $user_cabang, $user_login, $user_logout ,$user_aktif ){
		//function users_update($user_id, $user_name, $user_karyawan, $user_log, $user_groups, $user_aktif ){
			if ($user_aktif=="")
				$user_aktif = "Y";
			
			$data = array(
				"user_id"=>$user_id,			
				"user_name"=>$user_name,
				"user_login"=>$user_login,
				"user_logout"=>$user_logout,
				//"user_passwd"=>md5($user_passwd),			
				"user_aktif"=>$user_aktif			
			);
			
	/*		$sql="SELECT karyawan_id FROM karyawan WHERE karyawan_id='".$user_karyawan."'";
			$rs=$this->db->query($sql);
			if($rs->num_rows())
				$data["user_karyawan"]=$user_karyawan;*/
			if($user_passwd!=="")
				$data["user_passwd"]=md5($user_passwd);
				
			$sql="SELECT group_id FROM tbl_s_usergroups WHERE group_id='".$user_groups."'";
			$rs=$this->db->query($sql);
			if($rs->num_rows())
				$data["user_groups"]=$user_groups;
				
			$sql = "SELECT branch_id FROM m_branch WHERE branch_id = '".$user_cabang."'";
			$res = $this->db->query($sql);
			if($res->num_rows())
				$data["user_cabang"]=$user_cabang;
			
			$this->db->where('user_id', $user_id);
			$this->db->update('tbl_s_users', $data);
			return '1';
			//return $this->db->last_query();
		}
		
		//function for create new record
		function users_create($user_name ,$user_passwd ,$user_karyawan ,$user_log ,$user_groups , 
								$user_cabang, $user_login, $user_logout,$user_aktif ){
		if ($user_aktif=="")
			$user_aktif = "Y";
			$data = array(
	
				"user_name"=>$user_name,	
				"user_passwd"=>md5($user_passwd),	
				"user_log"=>$user_log,
				"user_login"=>$user_login,
				"user_logout"=>$user_logout,
				"user_groups"=>$user_groups,	
				"user_aktif"=>$user_aktif	
			);
			
				
			$this->db->insert('tbl_s_users', $data); 
			if($this->db->affected_rows())
				return '1';
			else
				return '0';
		}
		
		//fcuntion for delete record
		function users_delete($pkid){
			// You could do some checkups here and return '0' or other error consts.
			// Make a single query to delete all of the userss at the same time :
			if(sizeof($pkid)<1){
				return '0';
			} else if (sizeof($pkid) == 1){
				$query = "DELETE FROM tbl_s_users WHERE user_id = ".$pkid[0];
				$this->db->query($query);
			} else {
				$query = "DELETE FROM tbl_s_users WHERE ";
				for($i = 0; $i < sizeof($pkid); $i++){
					$query = $query . "user_id= ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$query = $query . " OR ";
					}     
				}
				$this->db->query($query);
			}
			if($this->db->affected_rows()>0)
				return '1';
			else
				return '0';
		}
		
		//function for advanced search record
		function users_search($user_name,$user_groups ,$user_aktif ,$start,$end){
			//full query
			$query="SELECT * FROM tbl_s_users,tbl_s_usergroups WHERE user_groups=group_id";
			
			if($user_name!=''){
				$query.=preg_match("/\bWHERE\b/i",$query)?" AND ":" WHERE ";
				$query.= " user_name LIKE '%".$user_name."%'";
			};
			
			if($user_groups!=''){
				$query.=preg_match("/\bWHERE\b/i",$query)?" AND ":" WHERE ";
				$query.= " user_groups ='".$user_groups."'";
			};
			
			if($user_aktif!=''){
				$query.=preg_match("/\bWHERE\b/i",$query)?" AND ":" WHERE ";
				$query.= " user_aktif ='".$user_aktif."'";
			};
			
			$result = $this->db->query($query);
			$nbrows = $result->num_rows();
			
			$limit = $query." LIMIT ".$start.",".$end;		
			$result = $this->db->query($limit);    
			
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}
		
		//function for print record
		function users_print($user_id ,$user_name  ,$user_karyawan ,$user_log ,$user_groups ,$user_aktif ,$option,$filter){
			//full query
			$query="SELECT * FROM tbl_s_users,tbl_s_usergroups WHERE user_groups=group_id";
			if($option=='LIST'){
				$query .=preg_match("/\bWHERE\b/i",$query)? " AND ":" WHERE ";
				$query .= " (user_name LIKE '%".addslashes($filter)."%' OR
							user_groups LIKE '%".addslashes($filter)."%' 
							 )";
				$result = $this->db->query($query);
			} else if($option=='SEARCH'){
				
				if($user_name!=''){
					$query.=preg_match("/\bWHERE\b/i",$query)?" AND ":" WHERE ";
					$query.= " user_name LIKE '%".$user_name."%'";
				};
				if($user_karyawan!=''){
					$query.=preg_match("/\bWHERE\b/i",$query)?" AND ":" WHERE ";
					$query.= " user_karyawan = '".$user_karyawan."'";
				};
				
				if($user_groups!=''){
					$query.=preg_match("/\bWHERE\b/i",$query)?" AND ":" WHERE ";
					$query.= " user_groups = '".$user_groups."'";
				};
				
				$result = $this->db->query($query);
			}
			return $result;
		}
		
		//function  for export to excel
		function users_export_excel($user_id ,$user_name ,$user_karyawan ,$user_log ,$user_groups ,$user_aktif ,$option,$filter){
			//full query
			$query="SELECT * FROM tbl_s_users,tbl_s_usergroups WHERE user_groups=group_id";
			if($option=='LIST'){
				$query .=preg_match("/\bWHERE\b/i",$query)? " AND ":" WHERE ";
				$query .= " ( user_name LIKE '%".addslashes($filter)."%'  OR
							  user_groups LIKE '%".addslashes($filter)."%' 
							   )";
				$result = $this->db->query($query);
			} else if($option=='SEARCH'){
				
				if($user_name!=''){
					$query.=preg_match("/\bWHERE\b/i",$query)?" AND ":" WHERE ";
					$query.= " user_name LIKE '%".$user_name."%'";
				};
				
				if($user_groups!=''){
					$query.=preg_match("/\bWHERE\b/i",$query)?" AND ":" WHERE ";
					$query.= " user_groups = '".$user_groups."'";
				};
				
				$result = $this->db->query($query);
			}
			return $result;
		}
		

}
?>